<template>
  <div>
    <el-dialog v-model="visible" :show-close="false">
      <template #header="{ close, titleId, titleClass }">
        <div class="my-header">
          <h4 :id="titleId" :class="titleClass">设置佣金比例</h4>
        </div>
      </template>

      <div>
        <el-form :model="formData" label-width="120px" ref="elFormRef">
          <el-form-item label="输入比例" prop="aa">
            <el-input-number min="0" max="100" v-model="formData.commission_rate" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="close">关闭</el-button>

          <el-button type="primary" @click="handleSubmit">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang='ts' setup>
import { ref, onMounted } from 'vue'
import { GoodsPriceAddAndEdit, SetEditUserRate } from '@/api/index'
import { ElMessage } from 'element-plus'

const visible = ref(false)

const elFormRef = ref(null)

const formType = ref('add')

const open = (config, type) => {
  Object.assign(formData.value, config.data)

  visible.value = true

  formType.value = type
}

const close = () => {
  visible.value = false
}

const formData = ref({
  uid: '',
  commission_rate: '',
})

const handleSubmit = async () => {
  try {
    // formData.value.method = formType.value

    const res = await SetEditUserRate(formData.value)

    ElMessage({
      message: `佣金比例${formType.value == 'edit' ? '修改' : '添加'}成功!`,
      type: 'success'
    })

    elFormRef.value.resetFields()

    emit('reload')

    close()
  } catch (err) {
    console.log('handleSubmit', err)

    ElMessage({
      message: '佣金比例设置失败!',
      type: 'error'
    })
  }
}

// 暴露方法给父组件
defineExpose({
  open
});

const emit = defineEmits(['reload'])

onMounted(() => {
  // open()
})
</script>

<style lang='scss' scoped></style>
